package cn.otrue.patienthealthmanager.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.CommonUtil;

import com.mhealth37.open.sdk.MhealthApplication;
import com.mhealth37.open.sdk.MhealthApplication.OnRequestMhealthListener;
import com.mhealth37.open.sdk.bluetooth.BluetoothConnector.ConnectStatusListener;
import com.mhealth37.open.sdk.bluetooth.BluetoothConnector.ExceptionListener;
import com.mhealth37.open.sdk.bluetooth.BluetoothConnector.MeasureDataListener;
import com.mhealth37.open.sdk.bluetooth.ConnectStatus;
import com.mhealth37.open.sdk.bluetooth.MeasureException;

public class AscertainPopupWindow extends PopupWindow implements
		OnRequestMhealthListener, ConnectStatusListener, MeasureDataListener,
		ExceptionListener, OnClickListener {
	private MhealthApplication mhealthApp;
	private Context mContext;
	private View mPopView;
	private int textSize;
	private Activity activity;
	private boolean isComplete = false;

	public AscertainPopupWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public AscertainPopupWindow(Activity activity,
			final GetPickResultListener getPickResultListener) {

		super(activity);
		this.activity = activity;
		mContext = activity;
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPopView = inflater.inflate(R.layout.item_pick_date_layout_measure,
				null);
		TextView tv_userinfo = (TextView) mPopView
				.findViewById(R.id.tv_userinfo);
		tv_userinfo.setText("用户id：100000 性别：0" + "出生时间"
				+ System.currentTimeMillis() / 1000 + " 身高175" + " 体重70"
				+ " 电话：13212035396");
		tv_connect = (TextView) mPopView.findViewById(R.id.tv_connect);
		tv_exception = (TextView) mPopView.findViewById(R.id.tv_exception);
		tv_measure = (TextView) mPopView.findViewById(R.id.tv_measure);
		tv_result = (TextView) mPopView.findViewById(R.id.tv_result);
		bt_connect = (Button) mPopView.findViewById(R.id.bt_connect);
		bt_measure = (Button) mPopView.findViewById(R.id.bt_measure);
		bt_upload = (Button) mPopView.findViewById(R.id.bt_upload);

		iv_catt_ascertain = (TextView) mPopView
				.findViewById(R.id.iv_catt_ascertain);
		iv_catt_falsh = (TextView) mPopView.findViewById(R.id.iv_catt_falsh);

		iv_catt_ascertain.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isComplete) {
					getPickResultListener.onGetResult(new Object[] { high_bp,
							low_bp, heartrate }, 1);
				} else {
					Toast.makeText(mContext, "测量还没完成", 0).show();
				}

			}
		});

		bt_connect.setEnabled(false);
		bt_measure.setEnabled(false);
		bt_connect.setOnClickListener(this);
		bt_measure.setOnClickListener(this);
		bt_upload.setOnClickListener(this);

	
		iv_catt_falsh.setOnClickListener(this);
		mhealthApp = MhealthApplication.getInstance(mPopView.getContext());
		mhealthApp.setOnRequestMhealthListener(this);

		textSize = CommonUtil.dip2px(mContext, 20);

		initWindowStyleByVersion(mPopView);

		// 设置SelectPicPopupWindow的View
		this.setContentView(mPopView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		this.setOutsideTouchable(true);

	}

	private void initWindowStyleByVersion(View mPopView2) {
		// 窗口标题容器
		RelativeLayout rl_catt_popTitle = (RelativeLayout) mPopView2
				.findViewById(R.id.rl_catt_popTitle);
		// 窗口标题
		TextView tv_catt_windowTitle = (TextView) mPopView2
				.findViewById(R.id.tv_catt_windowTitle);

		// int dip2px = dip2px(mContext, 18);
		rl_catt_popTitle.setBackgroundColor(mContext.getResources().getColor(
				R.color.window_bg_children));
		tv_catt_windowTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		// tv_catt_cancle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

	}

	@Override
	public void onClick(View arg0) {

		if (arg0 == bt_connect) {
			mhealthApp.beginConnect();
			bt_connect.setEnabled(false);
		} else if (arg0 == bt_measure) {
			mhealthApp.beginMeasure();
		} else if (arg0 == bt_upload) {
			mhealthApp.uploadUserInfo("100000", "0", System.currentTimeMillis()
					/ 1000 + "", "175", "70", "13212035396");
		} else if (arg0 == iv_catt_falsh) {
			dismiss();
		}

	}

	private int high_bp;
	private int low_bp;
	private int heartrate;
	private TextView tv_connect;
	private TextView tv_exception;
	private TextView tv_measure;
	private TextView tv_result;
	private Button bt_connect;
	private Button bt_measure;
	private Button bt_upload;
	private TextView iv_catt_ascertain;
	private TextView iv_catt_falsh;

	@Override
	public void onProcessDataChanged(final int bloodpress, final int heartbeat) {
		activity.runOnUiThread(new Runnable() {
			public void run() {
				tv_measure.setText("血压：" + bloodpress + " 心率：" + heartbeat);

			}
		});

	}

	@Override
	public void onResultDataChanged(final int high, final int low,
			final int heart) {

		activity.runOnUiThread(new Runnable() {
			public void run() {
				high_bp = high;
				low_bp = low;
				heartrate = heart;
				tv_result.setText("高压：" + high + " 低压：" + low + " 心率：" + heart);
				isComplete = true;

			}
		});

	}

	@Override
	public void onConnectStatusChanged(final ConnectStatus status) {
		isComplete = false;
		activity.runOnUiThread(new Runnable() {
			public void run() {
				switch (status) {
				case STATUS_MEASURE_PREPARED:
					bt_connect.setEnabled(false);
					bt_measure.setEnabled(true);
					break;
				case STATUS_PAIRING:
					break;
				case STATUS_CONNECTED:
					break;
				case STATUS_PAIRED:
					break;
				default:
					break;
				}
				tv_connect.setText(status.toString());

			}
		});

	}

	@Override
	public void returnRequestMsg(int requestCode, String body) {
		isComplete = false;
		if (body.contains("success")) {
			Toast.makeText(mPopView.getContext(),
					mPopView.getContext().getString(R.string.msg_open_success),
					0).show();
			mhealthApp.registerListeners(this, this, this);

			activity.runOnUiThread(new Runnable() {
				public void run() {
					bt_connect.setEnabled(true);
				}
			});

		}

		// TODO Auto-generated method stub

	}

	@Override
	public void onExceptionOcurred(final MeasureException exception) {
		isComplete = false;
		activity.runOnUiThread(new Runnable() {
			public void run() {

				switch (exception.getType()) {
				case TYPE_CONNECT_DISRUPTED:
					bt_connect.setEnabled(true);
					bt_measure.setEnabled(false);
					break;

				default:
					break;
				}
				tv_exception.setText(exception.getMessage());

			}
		});
	}

}
